//
//  ALTabbarController.m
//  AwesomeLoan
//
//  Created by tanxiaokang on 2017/11/23.
//  Copyright © 2017年 RunNong. All rights reserved.
//

#import "ALTabbarController.h"
#import "LTRepaymentCenterViewController.h"
#import "LTHomeViewController.h"
#import "LTUserCenterViewController.h"

@interface ALTabbarController ()<UITabBarControllerDelegate>

@end

@implementation ALTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate=self;
}

- (void)CustomUINavigationBar {
    
    //    if (@available(iOS 11.0, *)) {
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"backImg"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backImg"]];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"naviBG"] forBarMetrics:UIBarMetricsDefault];
    //    } else {
    //    [[UINavigationBar appearance] setShadowImage:[UIImage new]];  // 控制iOS11导航栏下面的横线
    [[UINavigationBar appearance] setTintColor:TEXT_33_COLOR];
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:TEXT_33_COLOR, NSForegroundColorAttributeName,[UIFont systemFontOfSize:18], NSFontAttributeName, nil]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]}forState:UIControlStateNormal];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (UIViewController *)addViewController:(UIViewController *)viewCon title:(NSString *)title andImage:(NSString *)imageName selectedImage:(NSString *)selectedImg tagInter:(NSInteger)tagInter{
    viewCon.title = title;
    
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    viewCon.tabBarItem = item;
    item.tag = tagInter;
    
    self.tabBar.translucent = YES;
    self.tabBar.tintColor = MAIN_COLOR;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewCon];
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.viewControllers];
    [array addObject:nav];
    self.viewControllers = array;
    
    // title离图片近一点
    [item setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
    //自定制导航栏
    [self CustomUINavigationBar];
    
    return viewCon;
}

#pragma mark - 定制标签栏
+ (ALTabbarController *)setUpCustomTabBarController {
    
    //主控制器
    ALTabbarController *tabBarCon = [[ALTabbarController alloc] init];
    
    // 首页
    [tabBarCon addViewController:[[LTHomeViewController alloc]init]
                           title:ALTabBarTitleCreditCardlnstance
                        andImage:@"homeTab_flow"
                   selectedImage:@"homeTab_flowSel"
                        tagInter:201];

    [tabBarCon addViewController:[[LTRepaymentCenterViewController alloc]init]
                           title:ALTabBarTitleflowCardlnstance
                        andImage:@"homeTab_borrow"
                   selectedImage:@"homeTab_borrowSel"
                        tagInter:201];
    
    // 我的
    [tabBarCon addViewController:[[LTUserCenterViewController alloc]init]
                           title:ALTabBarTitleUserCenterlnstance
                        andImage:@"homeTab_my"
                   selectedImage:@"homeTab_mySel"
                        tagInter:201];
    
    return tabBarCon;
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:ALHomeHiddenNavNotification object:nil];
}

//禁止tab多次点击
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    UIViewController *tbselect=tabBarController.selectedViewController;
    if([tbselect isEqual:viewController]){
        return NO;
    }
    return YES;
}

///
///注意此代码需在UITabBarViewConroller派生类中实现
#pragma mark - 返回到首页
-(void)retBorrowVCMethod {
    
    UINavigationController *oldNavigationController = [self.viewControllers objectAtIndex:self.selectedIndex];
    
    NSUInteger conInteger = oldNavigationController.viewControllers.count - 1;
    
    int myLenght = [[NSString stringWithFormat:@"%lu",(unsigned long)conInteger] intValue];
    
    for (int i = myLenght; i >= 0; i--) {
        
        UIViewController *viewController = [oldNavigationController.viewControllers objectAtIndex:i];
        
        [oldNavigationController popToViewController:viewController animated:NO];
    }
    
    //当不是UITabBarViewController不是第1项时，就强制切换到Item1
    
    if (0 != self.selectedIndex){
        
        self.selectedIndex = 0;
    }
    
    //    UINavigationController *newNavigationController = [self.viewControllers objectAtIndex:0];
    //
    //    ViewController7 *viewcontroller7 = [newNavigationController.viewControllers objectAtIndex:0];
    
}


@end
